﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fdb;
using Futil;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
using System.Reflection;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
using IntaiDebug;

namespace intadb
{
    public partial class FrmExtend : Form
    {
        public SynchronizationContext SyncContext;
        public FrmExtend()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            this.SyncContext = SynchronizationContext.Current;
            Thread thread = new Thread(new ThreadStart(this.sync))
            {
                Priority = ThreadPriority.Normal,
                IsBackground = true
            };

            thread.Start();
        }

        public void SetbtnText(string txt)
        {
            if (txt.IndexOf("转换中...")>-1)
            {
                this.btnConvert.Text = txt;
                this.btnConvert.Enabled = false;
            }
            else if (txt.IndexOf("转换完成")>-1)
            {
                this.btnConvert.Text = "开始转换";
                this.btnConvert.Enabled = true;
                MessageBox.Show(txt);
            }
            else
            {
                this.btnConvert.Text = "开始转换";
                this.btnConvert.Enabled = true;
                MessageBox.Show(txt);
            }
        }

        private void SetbtnTextHnd(object o)
        {
            ReflectHelper.MethodInvoke(this, "SetbtnText", new object[] { o });
        }

        public void SetJYBbtnText(string txt)
        {
            if (txt.IndexOf("入库中...") > -1)
            {
                this.btnJYBHisotryHandle.Text = txt;
                this.btnJYBHisotryHandle.Enabled = false;
            }
            else if (txt.IndexOf("入库完成") > -1)
            {
                this.btnJYBHisotryHandle.Text = "机油泵-历史数据合并处理";
                this.btnJYBHisotryHandle.Enabled = true;
                this.txtContent.Text = txt;
            }
            else
            {
                this.btnJYBHisotryHandle.Text = "机油泵-历史数据合并处理";
                this.btnJYBHisotryHandle.Enabled = true;
                this.txtContent.Text = txt;
                //MessageBox.Show(txt);
            }
        }

        private void SetJYBbtnTextHnd(object o)
        {
            ReflectHelper.MethodInvoke(this, "SetJYBbtnText", new object[] { o });
        }

        public void SetJYBCommonbtnText(string txt)
        {
            if (txt.IndexOf("入库中...") > -1)
            {
                this.btnJYBHisotryCommonHandle.Text = txt;
                this.btnJYBHisotryCommonHandle.Enabled = false;
            }
            else if (txt.IndexOf("入库完成") > -1)
            {
                this.btnJYBHisotryCommonHandle.Text = "机油泵-历史数据常规处理";
                this.btnJYBHisotryCommonHandle.Enabled = true;
                this.txtContent.Text = txt;
            }
            else
            {
                this.btnJYBHisotryCommonHandle.Text = "机油泵-历史数据常规处理";
                this.btnJYBHisotryCommonHandle.Enabled = true;
                this.txtContent.Text = txt;
                //MessageBox.Show(txt);
            }
        }

        private void SetJYBCommonbtnTextHnd(object o)
        {
            ReflectHelper.MethodInvoke(this, "SetJYBCommonbtnText", new object[] { o });
        }

        public void sync()
        {
            try
            {
                this.SyncContext.Send(SetbtnTextHnd, "转换中...");
                BaseDbContext dbcontext = new BaseDbContext();
                dbcontext.Object.ConnectionString = "intadbconn";
                dbcontext.Object.DatabaseType = DatabaseType.SQLite;
                List<DateTime> datelist = new List<DateTime>();
                for (var month = 1; month < 13; month++)
                {
                    string monthstr = (month < 10 ? "0" : "") + month;
                    string sql = "select * from intadata where dateandtime like '2022" + monthstr + "%'";
                    DataTable dt = dbcontext.Object.FetchDataTable(sql);
                    foreach (DataRow dr in dt.Rows)
                    {
                        object timeobj = dr["dateandtime"];
                        if (timeobj != null)
                        {
                            if (!string.IsNullOrEmpty(timeobj.ToString()))
                            {
                                datelist.Add(Convert.ToDateTime(timeobj.ToString()));
                            }
                        }
                    }
                }

                this.SyncContext.Send(SetbtnTextHnd, "转换中...总计：" + datelist.Count);
                int index = 0;
                foreach (DateTime _time in datelist)
                {
                    string old_datetime = _time.ToString("yyyyMMddHHmmss");
                    string new_datetime = _time.ToString("yyyy-MM-dd HH:mm:ss");
                    string sql1 = "select * from intadata where dateandtime='" + old_datetime + "'";
                    //DataTable dt1 = dbcontext.Object.FetchDataTable(sql1);
                    string sql2 = "update intadata set dateandtime='" + new_datetime + "' where dateandtime='" + old_datetime + "'";
                    ExecuteResult er = dbcontext.Object.ExecuteSQLWithResult(sql2);
                    if (!er.Success)
                    {
                        this.SetbtnText("失败：" + er.ResultString);
                        return;
                    }
                    index++;
                    this.SyncContext.Send(SetbtnTextHnd, "转换中..." + index + "/" + datelist.Count);
                }

                BaseDbContext dbcontext2 = new BaseDbContext();
                dbcontext2.Object.ConnectionString = "intadbconn2";
                dbcontext2.Object.DatabaseType = DatabaseType.SQLite;
                string sql3 = "select * from intadata where dateandtime between '2023-02-20 17:57:15' and '2023-02-21 11:42:08'";
                DataTable dt1 = dbcontext2.Object.FetchDataTable(sql3);
                foreach (DataRow dr in dt1.Rows)
                {
                    string sql4 = "insert into intadata(code,workstation,leakrate,result,dateandtime";
                    for (var i = 1; i < 21; i++)
                    {
                        sql4 += ",ex"+i;
                    }
                    sql4 += ") values(";
                    sql4 += "'" + dr["code"] + "',";
                    sql4 += "'" + dr["workstation"] + "',";
                    sql4 += "'" + dr["leakrate"] + "',";
                    sql4 += "'" + dr["result"] + "',";
                    sql4 += "'" + Convert.ToDateTime(dr["dateandtime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'";
                    for (var i = 1; i < 21; i++)
                    {
                        sql4 += ",'" + dr["ex"+i]+"'";
                    }
                    sql4 += ")";
                    ExecuteResult er = dbcontext.Object.ExecuteSQLWithResult(sql4);
                    if (!er.Success)
                    {
                        this.SetbtnText("失败：" + er.ResultString);
                        return;
                    }
                }

                this.SyncContext.Send(SetbtnTextHnd, "转换完成 总计：" + datelist.Count + " 导入" + dt1.Rows.Count+"条数据");
            }catch(Exception ex){
                this.SetbtnText("失败：" + ex.ToString());
            }
        }

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private void btnopendoc_Click(object sender, EventArgs e)
        {
            ////c:\\激光打标软件\\激光打标软件\\ezcad_lite_2.14.16(20210519)\ezcad_lite_2.14.16(20210519)\ezcad2.exe
            //string filename = this.txtfolder.Text.Trim();
            //string args = this.txtPath.Text.Trim();
            ////System.Diagnostics.Process.Start("C:\\WINDOWS\\system32\\mspaint.exe", ".//pikaqiu.jpg");
            //System.Diagnostics.Process.Start(filename, args);

            Process myProcess = new Process();
            myProcess.StartInfo.UseShellExecute = true;
            myProcess.StartInfo.FileName = @"c:\\激光打标软件\\激光打标软件\\ezcad_lite_2.14.16(20210519)\ezcad_lite_2.14.16(20210519)\ezcad2.exe";
            //myProcess.StartInfo.Arguments = @"c:\\无标题.ezd";
            myProcess.StartInfo.CreateNoWindow = false;
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
            myProcess.Start();
            
            IntPtr ptr = IntPtr.Zero;
            while ((ptr = myProcess.MainWindowHandle) == IntPtr.Zero)
            { 
                myProcess.WaitForInputIdle(1 * 1000);
                Thread.Sleep(10);
            }
            //MessageBox.Show(myProcess.MainWindowHandle.ToString());
            ShowWindow(myProcess.MainWindowHandle, 11);
        }

        private void btnclosedoc_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtext.Text.Trim()))
                {
                    return;
                }
                Process[] processes = Process.GetProcesses();
                foreach (Process p in processes)
                {
                    if (p.ProcessName.ToLower().IndexOf(this.txtext.Text.Trim()) > -1)
                    {
                        p.Kill();
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        public static byte[] ByteCut(byte[] value, byte cut = 0x0D)
        {
            var list = new List<byte>();
            list.AddRange(value);
            for (var i = list.Count - 1; i >= 0; i--)
            {
                if (list[i] == cut)
                    list.RemoveAt(i);
            }
            var lastbyte = new byte[list.Count];
            for (var i = 0; i < list.Count; i++)
            {
                lastbyte[i] = list[i];
            }
            return lastbyte;
        }

        private void btnJYSMQDATA_Click(object sender, EventArgs e)
        {
            List<byte> scanBytes1 = new List<byte>();
            byte[] buffer = null;
            List<byte> jllist = new List<byte>();
            string content = this.txtContent.Text.Trim();
            string[] arr = content.Split('-');
            if (arr.Length > 0)
            {
                List<string> strlist = arr.ToList();
                foreach (var str in strlist)
                {
                    jllist.Add(Convert.ToByte("0x" + str, 16));
                }

                buffer = jllist.ToArray();
            }
            try
            {
                if (buffer != null)
                {
                    scanBytes1.AddRange(buffer);
                    if (scanBytes1.Contains(0x0D))
                    {

                        List<byte> scanBytes11 = scanBytes1.GetRange(0, scanBytes1.Count - 1);
                        string str = Encoding.ASCII.GetString(scanBytes11.ToArray());
                        IOHelper.DebugMsg.Log("扫码枪2获取值" + str);
                        if (!string.IsNullOrEmpty(str.Trim()))
                        {
                            MessageBox.Show(str);
                        }

                        scanBytes1.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                IOHelper.DebugMsg.Log("扫码枪读取数据 Dev_OnDevData5 error:" + ex.Message + ex.StackTrace);
            }
        }

        private void btnJYBHisotryHandle_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认合并处理吗？", "是否处理？", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                this.SyncContext = SynchronizationContext.Current;
                Thread thread = new Thread(new ThreadStart(this.JYB_HistoryDataHandle_Sync))
                {
                    Priority = ThreadPriority.Normal,
                    IsBackground = true
                };

                thread.Start();
            };
        }

        /// <summary>
        /// 机油泵历史数据处理（将多条数据合并为一条数据上传至MES中间库）
        /// </summary>
        public void JYB_HistoryDataHandle_Sync()
        {
            BaseDbContext sqlitedbcontext = new BaseDbContext();
            sqlitedbcontext.Object.ConnectionString = "sqlite_intadbconn";
            sqlitedbcontext.Object.DatabaseType = DatabaseType.SQLite;

            BaseDbContext sqlserverdbcontext = new BaseDbContext();
            sqlserverdbcontext.Object.ConnectionString = "sqlserver_intadbconn";
            sqlserverdbcontext.Object.DatabaseType = DatabaseType.SqlServer;
            string lastCode = "";

            try
            {
                string sql = "select distinct code from intadata where code<>'' and code not like 'http://%' and ex1 like '%测试%' and ex2 like '%min%' and ex6='--' and ex7='--' and ex8='--' and ex9='--' and ex10='--' and dateandtime>'2022-04-01 00:00:00'";
                DataTable dt = sqlitedbcontext.Object.FetchDataTable(sql);
                this.SyncContext.Send(SetJYBbtnTextHnd, "入库中...总计：" + dt.Rows.Count);
                int index = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    object code = dr["code"];
                    if (code != null)
                    {
                        lastCode = code.ToString();
                        DataTable dt2 = sqlitedbcontext.Object.FetchDataTable("select * from intadata where code='" + code + "' and ex1 like '%测试%' and ex2 like '%min%' and ex6='--' and ex7='--' and ex8='--' and ex9='--' and dateandtime>'2022-04-01 00:00:00' order by dateandtime desc");
                        int needAddCount = 0;
                        if (dt2.Rows.Count < 8)
                        {
                            needAddCount = 8 - dt2.Rows.Count;
                            for (var i=0;i < needAddCount; i++)
                            {
                                DataRow newDr = dt2.NewRow();
                                newDr["code"] = code.ToString();
                                dt2.Rows.InsertAt(newDr,0);
                            }
                        }

                        string dateandtime = dt2.Rows[needAddCount]["dateandtime"].ToString();
                        string ex15 = FmtExValue(dt2.Rows[0]["ex2"]);
                        string ex16 = dt2.Rows[0]["ex3"].ToString();

                        string ex13 = FmtExValue(dt2.Rows[1]["ex2"]);
                        string ex14 = dt2.Rows[1]["ex3"].ToString();

                        string ex11 = FmtExValue(dt2.Rows[2]["ex2"]);
                        string ex12 = dt2.Rows[2]["ex3"].ToString();

                        string ex9 = FmtExValue(dt2.Rows[3]["ex2"]);
                        string ex10 = dt2.Rows[3]["ex3"].ToString();

                        string ex7 = FmtExValue(dt2.Rows[4]["ex2"]);
                        string ex8 = dt2.Rows[4]["ex3"].ToString();

                        string ex5 = FmtExValue(dt2.Rows[5]["ex2"]);
                        string ex6 = dt2.Rows[5]["ex3"].ToString();

                        string ex3 = FmtExValue(dt2.Rows[6]["ex2"]);
                        string ex4 = dt2.Rows[6]["ex3"].ToString();

                        string ex1 = FmtExValue(dt2.Rows[7]["ex2"]);
                        string ex2 = dt2.Rows[7]["ex3"].ToString();

                        string ex17 = "工位1";
                        string ex18 = "bva2020泵盖";

                        string ex19 = "M000";
                        string ex20 = "";

                        string insertSql = "insert into intadata values(";
                        insertSql += "'" + code + "','','','','" + dateandtime + "',";
                        insertSql += "'" + ex1 + "','" + ex2 + "',";
                        insertSql += "'" + ex3 + "','" + ex4 + "',";
                        insertSql += "'" + ex5 + "','" + ex6 + "',";
                        insertSql += "'" + ex7 + "','" + ex8 + "',";
                        insertSql += "'" + ex9 + "','" + ex10 + "',";
                        insertSql += "'" + ex11 + "','" + ex12 + "',";
                        insertSql += "'" + ex13 + "','" + ex14 + "',";
                        insertSql += "'" + ex15 + "','" + ex16 + "',";
                        insertSql += "'" + ex17 + "','" + ex18 + "',";
                        insertSql += "'" + ex19 + "','" + ex20 + "')";

                        ExecuteResult er = sqlserverdbcontext.Object.ExecuteSQLWithResult(insertSql);
                        if (er.Success)
                        {
                            index++;
                            this.SyncContext.Send(SetJYBbtnTextHnd, "入库中..." + index + "/" + dt.Rows.Count);
                        }
                        else
                        {
                            this.SyncContext.Send(SetJYBbtnTextHnd, "失败：" + er.ResultString);
                        }
                    }
                }

                this.SyncContext.Send(SetJYBbtnTextHnd, "入库完成 总计：" + dt.Rows.Count);
            }
            catch(Exception ex)
            {
                this.SyncContext.Send(SetJYBbtnTextHnd, lastCode+"失败：" + ex.ToString());
            }
        }

        public string FmtExValue(object objValue)
        {
            if (objValue == null)
            {
                return string.Empty;
            }

            if (string.IsNullOrEmpty(objValue.ToString()))
            {
                return string.Empty;
            }

            string[] arr = objValue.ToString().Split(' ');
            string val = arr[0];
            val = val.Replace("+", "").Replace("-", "");

            try
            {
                double ss = Convert.ToDouble(val);
            }
            catch
            {
                throw new Exception("泄漏值非数字");
            }

            return val;
        }

        private void btnJYBHisotryCommonHandle_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认常规处理吗？", "是否处理？", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                this.SyncContext = SynchronizationContext.Current;
                Thread thread = new Thread(new ThreadStart(this.JYB_HistoryDataCommonHandle_Sync))
                {
                    Priority = ThreadPriority.Normal,
                    IsBackground = true
                };

                thread.Start();
            };
        }

        public void JYB_HistoryDataCommonHandle_Sync()
        {
            BaseDbContext sqlitedbcontext = new BaseDbContext();
            sqlitedbcontext.Object.ConnectionString = "sqlite_intadbconn";
            sqlitedbcontext.Object.DatabaseType = DatabaseType.SQLite;

            BaseDbContext sqlserverdbcontext = new BaseDbContext();
            sqlserverdbcontext.Object.ConnectionString = "sqlserver_intadbconn";
            sqlserverdbcontext.Object.DatabaseType = DatabaseType.SqlServer;
            string lastCode = string.Empty;

            try
            {
                string sql = "select * from intadata where code<>'' and code not like 'http://%' and code not like 'test%'";
                DataTable dt = sqlitedbcontext.Object.FetchDataTable(sql);
                this.SyncContext.Send(SetJYBCommonbtnTextHnd, "入库中...总计：" + dt.Rows.Count);
                int index = 0;
                int hasindex = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    lastCode = dr["code"].ToString();
                    string dateandtime = dr["dateandtime"].ToString();

                    DataTable hasDt=sqlserverdbcontext.Object.FetchDataTable("select * from intadata where code like '"+ lastCode+ "' and DateAndTime='"+ dateandtime+"'");
                    if(hasDt!=null&& hasDt.Rows.Count > 0)
                    {
                        hasindex++;
                        continue;
                    }

                    string ex1 = FmtExValue(dr["ex1"]);
                    string ex2 = dr["ex2"].ToString();

                    string ex3 = FmtExValue(dr["ex3"]);
                    string ex4 = dr["ex4"].ToString();

                    string ex5 = FmtExValue(dr["ex5"]);
                    string ex6 = dr["ex6"].ToString();

                    string ex7 = FmtExValue(dr["ex7"]);
                    string ex8 = dr["ex8"].ToString();

                    string ex9 = FmtExValue(dr["ex9"]);
                    string ex10 = dr["ex10"].ToString();

                    string ex11 = FmtExValue(dr["ex11"]);
                    string ex12 = dr["ex12"].ToString();

                    string ex13 = FmtExValue(dr["ex13"]);
                    string ex14 = dr["ex14"].ToString();

                    string ex15 = FmtExValue(dr["ex15"]);
                    string ex16 = dr["ex16"].ToString();

                    string ex17 = "工位1";
                    string ex18 = "bva2020泵盖";

                    string ex19 = "M000";
                    string ex20 = "";

                    string insertSql = "insert into intadata values(";
                    insertSql += "'" + lastCode + "','','','','" + dateandtime + "',";
                    insertSql += "'" + ex1 + "','" + ex2 + "',";
                    insertSql += "'" + ex3 + "','" + ex4 + "',";
                    insertSql += "'" + ex5 + "','" + ex6 + "',";
                    insertSql += "'" + ex7 + "','" + ex8 + "',";
                    insertSql += "'" + ex9 + "','" + ex10 + "',";
                    insertSql += "'" + ex11 + "','" + ex12 + "',";
                    insertSql += "'" + ex13 + "','" + ex14 + "',";
                    insertSql += "'" + ex15 + "','" + ex16 + "',";
                    insertSql += "'" + ex17 + "','" + ex18 + "',";
                    insertSql += "'" + ex19 + "','" + ex20 + "')";

                    ExecuteResult er = sqlserverdbcontext.Object.ExecuteSQLWithResult(insertSql);
                    if (er.Success)
                    {
                        index++;
                        this.SyncContext.Send(SetJYBCommonbtnTextHnd, "入库中..." + index + "/" + dt.Rows.Count);
                    }
                    else
                    {
                        this.SyncContext.Send(SetJYBCommonbtnTextHnd, "失败：" + er.ResultString);
                    }
                }

                this.SyncContext.Send(SetJYBCommonbtnTextHnd, "入库完成 总计：" + dt.Rows.Count+" 新入库:" + index + " 已存在:" + hasindex);
            }
            catch (Exception ex)
            {
                this.SyncContext.Send(SetJYBCommonbtnTextHnd, lastCode + "失败：" + ex.ToString());
            }
        }

        private void btnMysqlConnTest_Click(object sender, EventArgs e)
        {
            BaseDbContext mysqldbcontext = new BaseDbContext();
            mysqldbcontext.Object.ConnectionString = "mysql_intadbconn";
            mysqldbcontext.Object.DatabaseType = DatabaseType.Mysql;
            ExecuteResult er = mysqldbcontext.Object.TestConnnection();
            if (er.Success)
            {
                MessageBox.Show("连接正常！");
            }
            else
            {
                MessageBox.Show("连接失败！" + er.ResultString);
            }
        }

        private void btn解析RFID_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtRFID.Text.Trim()))
            {
                MessageBox.Show("rfidcode不能为空！");
                this.txtRFID.Focus();
                return;
            }
            //TSC 
            //02-03-08-00-65-17-48-76-E8-02-00-D7-A0
            //List<byte> lst = new List<byte>();
            //lst.Add(0x02);
            //lst.Add(0x03);
            //lst.Add(0x08);
            //lst.Add(0x00);
            //lst.Add(0x65);
            //lst.Add(0x17);
            //lst.Add(0x48);
            //lst.Add(0x76);
            //lst.Add(0xE8);
            //lst.Add(0x02);
            //lst.Add(0x00);
            //lst.Add(0xD7);
            //lst.Add(0xA0);
            //string code = Test.getRFIDSN(lst);
            string code = Convert.ToInt64(this.txtRFID.Text.Trim(), 16).ToString();
            MessageBox.Show(code.PadLeft(12, '0'));
        }

        private void btnCRC16_Click(object sender, EventArgs e)
        {
            //01 - 03 - 04 - FF - FF - FF - FE - 3A - 67
            //01-03-04-00-00-00-9E-7B-9B
            //01-03-04-00-00-00-9B-BB-98
            //01-03-04-FF-FF-FF-F8-BA-65
            //byte[] recbuffer = new byte[9] { 0x01,0x03,0x04,0xFF,0xFF,0xFF,0xFE,0x3A,0x67};
            //byte[] recbuffer = new byte[9] { 0x01, 0x03, 0x04, 0x00, 0x00, 0x00, 0x9E, 0x7B, 0x9B };
            //byte[] recbuffer = new byte[9] { 0x01, 0x03, 0x04, 0x00, 0x00, 0x00, 0x9B, 0xBB, 0x98 };
            byte[] recbuffer = new byte[9] { 0x01, 0x03, 0x04, 0xFF, 0xFF, 0xFF, 0xF8, 0xBA, 0x65 };
            byte[] databuffer = new byte[7];
            Array.Copy(recbuffer, 0, databuffer, 0, 7);
            byte[] crc16 = CRC.CRC16(databuffer);
            if (crc16 == null || crc16.Length != 2)
            {
                MessageBox.Show("CRC校验失败！");
                IOHelper.DebugMsg.Log("CRC校验失败 null");
                return;
            }

            if (crc16[0] != recbuffer[7] || crc16[1] != recbuffer[8])
            {
                MessageBox.Show("CRC校验失败！");
                IOHelper.DebugMsg.Log("CRC校验失败");
                return;
            }

            MessageBox.Show("CRC校验成功！");
        }

        //public void sync()
        //{
        //    this.SyncContext.Send(SetbtnTextHnd, "转换中...");
        //    BaseDbContext dbcontext = new BaseDbContext();
        //    dbcontext.Object.ConnectionString = "intadbconn";
        //    dbcontext.Object.DatabaseType = DatabaseType.SQLite;
        //    string sql = "select * from intadata where dateandtime like '2022%'";
        //    DataTable dt = dbcontext.Object.FetchDataTable(sql);
        //    this.SyncContext.Send(SetbtnTextHnd, "转换中...总计："+dt.Rows.Count);
        //    int index = 0;
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        object timeobj = dr["dateandtime"];
        //        if (timeobj != null)
        //        {
        //            if (!string.IsNullOrEmpty(timeobj.ToString()))
        //            {
        //                DateTime _time = Convert.ToDateTime(timeobj.ToString());
        //                string old_datetime = _time.ToString("yyyyMMddHHmmss");
        //                string new_datetime = _time.ToString("yyyy-MM-dd HH:mm:ss");
        //                string sql1 = "select * from intadata where dateandtime='" + old_datetime + "'";
        //                //DataTable dt1 = dbcontext.Object.FetchDataTable(sql1);
        //                string sql2 = "update intadata set dateandtime='" + new_datetime + "' where dateandtime='" + old_datetime + "'";
        //                ExecuteResult er = dbcontext.Object.ExecuteSQLWithResult(sql2);
        //                if (!er.Success)
        //                {
        //                    this.SetbtnText("失败："+er.ResultString);
        //                    return;
        //                }
        //                index++;
        //                this.SyncContext.Send(SetbtnTextHnd, "转换中..." + index +"/"+ dt.Rows.Count);
        //            }
        //        }
        //    }

        //    this.SyncContext.Send(SetbtnTextHnd, "转换完成 总计：" + dt.Rows.Count);
        //}
    }
}
